var express = require("express");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var fs = require("fs");

var app = express();

app.use(express.static("www"));
app.use(bodyParser.urlencoded({extended:false}));
app.use(cookieParser());


var filePath = "./stu.json";
var stus;
if(fs.existsSync(filePath)){
	var s = fs.readFileSync(filePath);
	stus = JSON.parse(s);
}else{
	stus = [];
}


//添加学生
app.post("/stu/add",function(req,res){
	var nameReg = /^[\u4e00-\u9fa5]+$/;
	if(!nameReg.test(req.body.name)){
		res.json({err:1,msg:"姓名不符合格式要求"});
		return;
	}
	var ageReg = /^\d{1,3}$/;
	if(!ageReg.test(req.body.age)){
		res.json({err:2,msg:"年龄不符合格式要求"});
		return;
	}
	if((req.body.gender!=0)&&(req.body.gender!=1)){
		res.json({err:3,msg:"性别不符合格式要求"});
		return;
	}
	var now = new Date();
	req.body.id=now.getTime();
	stus.push(req.body);
	var s = JSON.stringify(stus);
	fs.writeFileSync(filePath,s);
	res.json({err:0,msg:"添加成功"});
	
});

//查询第n页学生
app.post("/stu/list",function(req,res){
	var count = req.body.count*1||10;
	var page = req.body.page*1||0;
	
	var stuData = stus.slice(count*page,count*page+count);
	
	var pageNumber = Math.ceil(stus.length/count); 
	
	
	res.json({err:0,pageNumber,stuData});
	
});


//搜索接口
app.post("/stu/search",function(req,res){
	var tempArr = stus;
	
	if(req.body.name){
		tempArr = tempArr.filter(function(s){
			return s.name.indexOf(req.body.name)>=0;
		});
	}
	
	if(req.body.age){
		tempArr = tempArr.filter(function(s){
			return s.age == req.body.age;
		});
	}
	
	if(req.body.gender==1||req.body.gender==0){
		tempArr = tempArr.filter(function(s){
			return s.gender == req.body.gender;
		});
	}
	
	if(req.body.tel){
		tempArr = tempArr.filter(function(s){
			return s.tel.indexOf(req.body.tel)==0;
		});
	}
	
	var count = req.body.count*1||10;
	var page = req.body.page*1||0;
	
	var stuData = tempArr.slice(count*page,count*page+count);
	
	var pageNumber = Math.ceil(tempArr.length/count); 
	
	res.json({err:0,pageNumber,stuData});
});


//修改学生
app.post("/stu/edit",function(req,res){
	var ageReg = /^\d{1,3}$/;
	if(!ageReg.test(req.body.age)){
		res.json({err:2,msg:"年龄不符合格式要求"});
		return;
	}
	if((req.body.gender!=0)&&(req.body.gender!=1)){
		res.json({err:3,msg:"性别不符合格式要求"});
		return;
	}
	
	var st = stus.find(function(s){
		return s.id == req.body.id;
	});
	
	if(!st){
		res.json({err:4,msg:"id错误"});
		return ;
	}else{
		
		st.age = req.body.age||st.age;
		st.gender = req.body.gender||st.gender;
		st.tel = req.body.tel||st.tel;
		st.email = req.body.email||st.email;
		st.detail = req.body.detail||st.detail;
		
		var s = JSON.stringify(stus);
		fs.writeFileSync(filePath,s);
		res.json({err:0,msg:"修改成功"});
	}
	
});

//删除学生
app.post("/stu/delete",function(req,res){
	
	var st = stus.find(function(s){
		return s.id == req.body.id;
	});
	
	if(!st){
		res.json({err:4,msg:"id错误"});
		return ;
	}else{
		var index = stus.indexOf(st);
		stus.splice(index,1);
		var s = JSON.stringify(stus);
		fs.writeFileSync(filePath,s);
		res.json({err:0,msg:"删除成功"});
	}
	
	
});



app.listen(8080,function () {
    console.log("服务器已开启");
});